package nuc.soft.A01.jzs.controller.problem;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nuc.soft.A01.jzs.model.general.GeneralUser;
import nuc.soft.A01.jzs.model.problem.ManyChooseProblem;
import nuc.soft.A01.jzs.model.user.Admin;
import nuc.soft.A01.jzs.model.user.Teacher;
import nuc.soft.A01.jzs.services.AdminService;
import nuc.soft.A01.jzs.services.TeacherService;
import nuc.soft.A01.jzs.services.problem.ManyChooseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/teacher/manychoose")
public class ManyChooseController {


	@Autowired
	private ManyChooseService mcs;
	@Autowired
	private TeacherService ts;
	@Autowired
	private AdminService as;
	
	@RequestMapping("/showsearch")
	public String getSearchProblem(HttpServletRequest request,
			HttpServletResponse response,String searchname)
			{
		ArrayList<ManyChooseProblem> manychooses=mcs.getSearchManyChoose(searchname);
		request.setAttribute("manychooses", manychooses);
		return "/problem/manychoose/index";
			}
	
	
	@RequestMapping("/showall")
	public String getAllproblem(HttpServletRequest request,
			HttpServletResponse response) {
		ArrayList<ManyChooseProblem> manychooses=mcs.getAllManyChoose();
		request.setAttribute("manychooses", manychooses);
		return "/problem/manychoose/index";
	}


	@RequestMapping("/showadd")
	public String showadd(HttpServletRequest request,
			HttpServletResponse response) {
		return "/problem/manychoose/add";
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response,
			ManyChooseProblem problem) {
		HttpSession session=request.getSession();
		GeneralUser gu=(GeneralUser) session.getAttribute("user");
		try
		{
			Teacher teacher=(Teacher)gu;
			problem.setTeacherid(teacher.getTeacherid());
		}
		catch(Exception ex)
		{
			Admin admin=(Admin)gu;
			problem.setTeacherid("admin_"+admin.getAdminid());
		}
		mcs.insert(problem);
		return "forward:/teacher/manychoose/showall";
	}

	@RequestMapping("/showedit")
	public String showedit(HttpServletRequest request,
			HttpServletResponse response, String theid) {
		
		ManyChooseProblem problem=mcs.getAManyChooseById(theid);
		request.setAttribute("editproblem", problem);
		return "/problem/manychoose/edit";
	}

	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,
			HttpServletResponse response, ManyChooseProblem problem) {
		HttpSession session=request.getSession();
		GeneralUser gu=(GeneralUser) session.getAttribute("user");
		try
		{
			Teacher teacher=(Teacher)gu;
			problem.setTeacherid(teacher.getTeacherid());
		}
		catch(Exception ex)
		{
			Admin admin=(Admin)gu;
			problem.setTeacherid("admin_"+admin.getAdminid());
		}
		mcs.update(problem);
		return "forward:/teacher/manychoose/showall";
	}
}
